package com.kctvjeju.web.common.util;

import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.springframework.util.StopWatch;

public class LoggerAdvice {
	
private Logger logger = Logger.getLogger(LoggerAdvice.class);
	
	public Object aroundLogger(ProceedingJoinPoint pjp) throws Throwable{
		// 핵심 로직 메소드가 실행된 시간을 info 로그로 남긴다.
		// 형식 : Target 객체 이름.method명 : 실행시간
		Object returnValue = null;
		StopWatch sw = new StopWatch();
		sw.start();
		try{
			returnValue = pjp.proceed();
			return returnValue;
		}catch(Throwable th){
			throw th;
		}
		finally{
			sw.stop();
			Signature sg = pjp.getSignature(); 
			String targetName = pjp.getTarget().getClass().getName()+"."+sg.getName();
			long time = sw.getTotalTimeMillis();
			logger.info(targetName + " : " + time/(double)1000 +" second");
		}
	}
}
